package com.example.service.global;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.user.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 管理员业务处理
 **/
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 新增
     */
    public void add(User user) {
        /**
         * 新增时，只能注册普通用户且根据数据结构的健壮性获取到的值不为空
         * 将获得的数据设置为新用户的账号，密码默认为123456，角色为ordinary-user
         */
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (ObjectUtil.isNotNull(dbUser)) {
            throw new CustomException("用户已存在！");
        }
        if (ObjectUtil.isEmpty(user.getPassword())) {
            user.setPassword("123456");
        }
        if (ObjectUtil.isEmpty(user.getUsername())) {
            user.setUsername(user.getUsername());
        }
        user.setRole("COMMON-USER");
        userMapper.insert(user);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    /**
     * 修改
     */
    public void updateById(User user) {
        userMapper.updateById(user);
    }

    /**
     * 根据ID查询
     */
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    /**
     * 分页查询
     * 在后端进行分页，使用数据库进行分页，提高性能
     */
    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectAll(user);
        return PageInfo.of(list);
    }

    /**
     * 登录
     */
    public User login(User user) {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException("用户不存在");
        }
        if (!user.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException("账号或密码错误");
        }
        return dbUser;
    }

    /**
     * 修改密码
     * 先验证一下输入的原密码是否正确，如果正确，则修改密码为新密码
     */
    public void updatePassword(User user) {
        System.out.println("====================="+user);
        User dbUser = userMapper.selectByUsername(user.getUsername());
        System.out.println("====================="+dbUser);
        if (ObjectUtil.isNull(dbUser)) {
            throw new CustomException("用户不存在");
        }
        if (!user.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException("原密码错误");
        }
        dbUser.setPassword(user.getNewPassword());
        userMapper.updateById(dbUser);
    }

    /**
     * 注册新的用户
     * @param user
     */
    public void register(User user) {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (ObjectUtil.isNotNull(dbUser)) {
            throw new CustomException("用户名已存在！");
        }
        user.setRole("COMMON-USER");
        userMapper.insert(user);
    }
}